#ifndef BINARYTEXTEDIT_H
#define BINARYTEXTEDIT_H

#include <QPlainTextEdit>

class BinaryTextEdit : public QPlainTextEdit
{
    Q_OBJECT
    Q_PROPERTY(QByteArray binaryData READ binaryData WRITE setBinaryData NOTIFY binaryDataChanged)
    Q_PROPERTY(EscapeMode escapeMode READ escapeMode WRITE setEscapeMode NOTIFY escapeModeChanged)

public:
    enum EscapeMode {
        None,
        CEscape,
        URLEscape,
        Hex,
        Base64
    };

    BinaryTextEdit(EscapeMode escapeMode = CEscape, QWidget *parent = 0);
    explicit BinaryTextEdit(QWidget *parent);

    Q_INVOKABLE inline QByteArray binaryData() {return _binaryData;}
    Q_INVOKABLE inline EscapeMode escapeMode() {return _escapeMode;}

    static QString escape(QByteArray data, EscapeMode escapeMode);
    static QByteArray unescape(QString data, EscapeMode escapeMode);

signals:
    void binaryDataChanged();
    void escapeModeChanged();

public slots:
    void setBinaryData(QByteArray data);
    void setEscapeMode(EscapeMode mode);

    void sync();

private:
    QByteArray _binaryData;
    EscapeMode _escapeMode;

    QString escape(QByteArray data) {return escape(data, _escapeMode);}
    QByteArray unescape(QString data){return unescape(data, _escapeMode);}

};

#endif // BINARYTEXTEDIT_H
